package com.ruoyi.statisticalCenter.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.domain.vo.InitTableVo;
import com.ruoyi.common.domain.vo.TableVo;
import com.ruoyi.common.service.TableService;
import com.ruoyi.logistic.domain.PageInfo;
import com.ruoyi.statisticalCenter.domain.vo.ScWeightFlowDirectionDispositionVo;
import com.ruoyi.statisticalCenter.domain.vo.ScWeightFlowDirectionMonitoringVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import com.ruoyi.statisticalCenter.mapper.ScWeightFlowDirectionMonitoringMapper;
import com.ruoyi.statisticalCenter.domain.ScWeightFlowDirectionMonitoring;
import com.ruoyi.statisticalCenter.service.IScWeightFlowDirectionMonitoringService;

import javax.annotation.Resource;

/**
 * 均重流向监控Service业务层处理
 *
 * @author pyx
 * @date 2024-03-01
 */
@Service
public class ScWeightFlowDirectionMonitoringServiceImpl implements IScWeightFlowDirectionMonitoringService {
    @Resource
    private ScWeightFlowDirectionMonitoringMapper scWeightFlowDirectionMonitoringMapper;
    @Resource
    private TableService tableService;

    @Override
    public R collect(ScWeightFlowDirectionMonitoringVo vo) {
        //获取配置参数
        ScWeightFlowDirectionDispositionVo scWeightFlowDirectionDispositionVo = scWeightFlowDirectionMonitoringMapper.selectDisposition();

        BeanUtils.copyProperties(scWeightFlowDirectionDispositionVo, vo);
        //返回数据
        //止损考核
        PageHelper.startPage(vo.getPageDate());
        List<ScWeightFlowDirectionMonitoring> list = scWeightFlowDirectionMonitoringMapper.collectStop(vo);
        //均重考核
        PageHelper.startPage(vo.getPageDate());
        List<ScWeightFlowDirectionMonitoring> homogeneousList = scWeightFlowDirectionMonitoringMapper.collectHomogeneous(vo);
        //均重考核
        PageHelper.startPage(vo.getPageDate());
        List<ScWeightFlowDirectionMonitoring> flowList = scWeightFlowDirectionMonitoringMapper.collectFlow(vo);
        for (ScWeightFlowDirectionMonitoring scWeightFlowDirectionMonitoring : list) {
            for (ScWeightFlowDirectionMonitoring weightFlowDirectionMonitoring : homogeneousList) {
                if (scWeightFlowDirectionMonitoring.getId().equals(weightFlowDirectionMonitoring.getId())){
                    scWeightFlowDirectionMonitoring.setStockCount(weightFlowDirectionMonitoring.getStockCount());
                    scWeightFlowDirectionMonitoring.setBaleWeight(weightFlowDirectionMonitoring.getBaleWeight());
                    scWeightFlowDirectionMonitoring.setPackingWarehouseWeight(weightFlowDirectionMonitoring.getPackingWarehouseWeight());
                    scWeightFlowDirectionMonitoring.setAverageWeightIndex(weightFlowDirectionMonitoring.getAverageWeightIndex());
                    scWeightFlowDirectionMonitoring.setWeightAssessmentStandard(weightFlowDirectionMonitoring.getWeightAssessmentStandard());
                    scWeightFlowDirectionMonitoring.setWeightAssessment(weightFlowDirectionMonitoring.getWeightAssessment());
                }
            }
            for (ScWeightFlowDirectionMonitoring weightFlowDirectionMonitoring : flowList) {
                if (scWeightFlowDirectionMonitoring.getId().equals(weightFlowDirectionMonitoring.getId())){
                    scWeightFlowDirectionMonitoring.setQinghaiVotes(weightFlowDirectionMonitoring.getQinghaiVotes());
                    scWeightFlowDirectionMonitoring.setGansuVotes(weightFlowDirectionMonitoring.getGansuVotes());
                    scWeightFlowDirectionMonitoring.setHainanVotes(weightFlowDirectionMonitoring.getHainanVotes());
                    scWeightFlowDirectionMonitoring.setNingxiaVotes(weightFlowDirectionMonitoring.getNingxiaVotes());
                    scWeightFlowDirectionMonitoring.setFlowTally(weightFlowDirectionMonitoring.getFlowTally());
                    scWeightFlowDirectionMonitoring.setFlowRatio(weightFlowDirectionMonitoring.getFlowTally()/Double.valueOf(scWeightFlowDirectionMonitoring.getStockCount()));
                    scWeightFlowDirectionMonitoring.setFlowDirectionTest(0.00);
                }
            }
        }
        List<TableVo> tableVos = new ArrayList<>();
        TableVo tableVo = new TableVo();
        tableVo.setFields("settleDate");
        tableVo.setTranslate("统计日期");
        tableVos.add(tableVo);

        tableVo = new TableVo();
        tableVo.setFields("contractArea");
        tableVo.setTranslate("承包区名称");
        tableVos.add(tableVo);

        tableVo = new TableVo();
        tableVo.setFields("stop");
        tableVo.setTranslate("止损考核");
        List<TableVo> tableSunVos = new ArrayList<>();
        String[] stop = {"dailyAssessWeight", "stopWeight", "settlementWeight", "assessmentStandard","stopLossAssessment"};
        InitTableVo initTableVo = new InitTableVo();
        initTableVo.setFields(stop);
        initTableVo.setCode("scWeight");
        tableSunVos = tableService.initTable(initTableVo);
        tableVo.setSunList(tableSunVos);
        tableVos.add(tableVo);

        tableVo = new TableVo();
        tableVo.setFields("weight");
        tableVo.setTranslate("均重考核");
        tableSunVos = new ArrayList<>();
        String[] weight = {"stockCount", "baleWeight", "packingWarehouseWeight", "averageWeightIndex","weightAssessmentStandard","weightAssessment"};
        initTableVo = new InitTableVo();
        initTableVo.setFields(weight);
        initTableVo.setCode("scWeight");
        tableSunVos = tableService.initTable(initTableVo);
        tableVo.setSunList(tableSunVos);
        tableVos.add(tableVo);

        tableVo = new TableVo();
        tableVo.setFields("flow");
        tableVo.setTranslate("流向考核");
        tableSunVos = new ArrayList<>();
        String[] flow = {"ningxiaVotes", "gansuVotes", "qinghaiVotes", "hainanVotes","flowTally","flowRatio","directionIndicator","flowDirectionTest"};
        initTableVo = new InitTableVo();
        initTableVo.setFields(flow);
        initTableVo.setCode("scWeight");
        tableSunVos = tableService.initTable(initTableVo);
        tableVo.setSunList(tableSunVos);
        tableVos.add(tableVo);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("list", PageInfo.info(list));
        jsonObject.put("fields", tableVos);

        if (list == null || list.size() == 0) {
            return R.fail("暂无数据");
        } else {
            return R.ok(jsonObject);
        }
//        return scWeightFlowDirectionMonitoringMapper.collect(vo);
    }

    @Override
    public R updateDisposition(ScWeightFlowDirectionDispositionVo scWeightFlowDirectionDispositionVo) {
        scWeightFlowDirectionDispositionVo.setId("1");
        return scWeightFlowDirectionMonitoringMapper.updateDisposition(scWeightFlowDirectionDispositionVo) > 0 ? R.ok("修改成功") : R.fail("修改失败");
    }

    @Override
    public R selectDisposition() {
        return R.ok(scWeightFlowDirectionMonitoringMapper.selectDisposition());
    }
}
